package com.atguigu.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.atguigu.eduservice.entity.EduSubject;
import com.atguigu.eduservice.entity.vo.ExcelSubjectData;
import com.atguigu.eduservice.entity.vo.OneSubjectVo;
import com.atguigu.eduservice.entity.vo.TwoSubjectVo;
import com.atguigu.eduservice.listener.SubjectExcelListener;
import com.atguigu.eduservice.mapper.EduSubjectMapper;
import com.atguigu.eduservice.service.EduSubjectService;
import com.atguigu.servicebase.handler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-04-03
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {
    //导入课程分类信息
    @Override
    public void addSubject(MultipartFile file, EduSubjectService eduSubjectService) {
        try {
            EasyExcel.read(file.getInputStream(),
                    ExcelSubjectData.class,
                    new SubjectExcelListener(eduSubjectService)).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
            throw new GuliException(20001,"导入课程分类信息失败");
        }
    }

//查询所有课程信息
    @Override
    public List<OneSubjectVo> getAllSubject() {
        //1、查询所有一级分类信息
        QueryWrapper<EduSubject>oneWrapper=new QueryWrapper<>();
        oneWrapper.eq("parent_id","0");
        List<EduSubject> oneSubjectList = baseMapper.selectList(oneWrapper);
        //2、查询所有二级分类信息
        QueryWrapper<EduSubject>twoWrapper=new QueryWrapper<>();
        twoWrapper.ne("parent_id","0");
        List<EduSubject> twoSubjectList = baseMapper.selectList(twoWrapper);
        //3、遍历一级集合进行封装
        List<OneSubjectVo>allSubjectList=new ArrayList<>();
        for (int i = 0; i < oneSubjectList.size(); i++) {
            //3.1  去除每一个oneSubject
            EduSubject oneSubject = oneSubjectList.get(i);
            //3.2  把eduSubject转化成onesubjectvo
            OneSubjectVo oneSubjectVo = new OneSubjectVo();
         //第一种方式
          /*  oneSubjectVo.setId(oneSubject.getId());
            oneSubjectVo.setTitle(oneSubject.getTitle());*/
          //将oneSubject复制给oneSubjectVo，两个类中的想要复制的属性尽量一致
            BeanUtils.copyProperties(oneSubject,oneSubjectVo);
            //3.3  oneSubjectVo  封装到集合中
            allSubjectList.add(oneSubjectVo);
            //4、遍历二级，与一级信息有关的二级进行封装
            List<TwoSubjectVo>twoSubjectVos=new ArrayList<>();
            for (int m = 0; m < twoSubjectList.size(); m++) {
                //4.1  取出每一个twoSubject
                EduSubject twoSubject = twoSubjectList.get(m);
                //4.2   判断二级和一级是否有关系
                if(twoSubject.getParentId().equals(oneSubject.getId())){
                    //4.3把EduSubject转化成TwoSubjectVo
                    TwoSubjectVo twoSubjectVo = new TwoSubjectVo();
                    BeanUtils.copyProperties(twoSubject,twoSubjectVo);
                    //4.4 封装数据
                    twoSubjectVos.add(twoSubjectVo);
                }
            }
            oneSubjectVo.setChildren(twoSubjectVos);
        }
        return allSubjectList;
    }
}
